/*
 * @Author: 大白
 * @Date: 2019-12-04 09:48:26
 * @LastEditors  : 大白
 * @LastEditTime : 2020-01-22 10:30:47
 * @Description: 文件描述
 * @FilePath: \cyf_admin\src\config\http.js
 */
import axios from 'axios'
import qs from 'qs'
axios.defaults.timeout = 50000;  
                      //响应时间
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=UTF-8';        //配置请求头
axios.defaults.baseURL = 'https://www.careoe.com:8443'
//POST传参序列化(添加请求拦截器)
axios.interceptors.request.use((config) => {
    if(config.method  === 'post'){
        
        config.data = qs.stringify(config.data);
    }
    return config;
},(error) =>{
    console.log('错误的传参')
    return Promise.reject(error);
});
export function fetchPost(url, params={}) {
    return axios({
        method:"POST",
        url:url,
        data:params,
    })
};

////返回一个Promise(发送get请求)
export function fetchGet(url, param) {
    return new Promise((resolve, reject) => {
        axios.get(url, {params: param})
            .then(response => {
                resolve(response)
            }, err => {
                reject(err)
            })
            .catch((error) => {
                reject(error)
            })
    })
}
export const baseUrl = process.env.API_ROOT
export default {
    fetchPost,
    fetchGet,
}